home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / ANS199A.ZIP / play.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-25  |  6.2 KB  |  245 lines

  1. // (C) copyright 1996 Sacha Prins
  2.  
  3. #include <fstream.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "\work\modemengine\modemengine.hpp"
  7.  
  8. int bits_per_sample=4;
  9. int device= 1;
  10. char * file_name= NULL;
  11. char * com_port= NULL;
  12. BOOL debug= FALSE;
  13.  
  14. void usage();
  15. void parse_arg(int argc, char * argv[]);
  16. void play();
  17. MODEM_ENGINE::MODEMRESPONSE response (MODEM_ENGINE & me, int timeout=600);
  18.  
  19. //*****************************************************************************
  20. int main (int argc, char * argv[]) {
  21.  
  22.  parse_arg(argc, argv);
  23.  
  24.  cout << "bits per sample: " << bits_per_sample << endl
  25.       << "device         : " << device << endl
  26.       << "file name      : " << file_name << endl
  27.       << "com port       : " << com_port << endl;
  28.  
  29.  play();
  30.  
  31. };
  32.  
  33.  
  34. //*****************************************************************************
  35. void play() {
  36.  
  37.  MODEM_ENGINE me(com_port);
  38.  
  39.  ifstream fin(file_name, ios::bin);
  40.  if (!fin.good()) {
  41.     usage();
  42.     exit(0);
  43.  }
  44.  
  45.  if (me.mResultCode () == MODEM_ENGINE::noerror) {
  46.  
  47.     cout << endl << "Initializing the modem..." << endl;
  48.  
  49.     me.mInitialize();
  50.     response (me);
  51.  
  52.     me.vmSetDevice(MODEM_ENGINE::voice);
  53.     response (me);
  54.  
  55.     me.vmSetSpeed();
  56.     response (me);
  57.  
  58.     me.vmSetSilenceDetection(FALSE);
  59.     response (me);
  60.  
  61.     switch (bits_per_sample) {
  62.       case 2:
  63.       me.vmSetBits(MODEM_ENGINE::two);
  64.       break;
  65.       case 3:
  66.       me.vmSetBits(MODEM_ENGINE::three);
  67.       break;
  68.       case 4:
  69.       me.vmSetBits(MODEM_ENGINE::four);
  70.       break;
  71.     } /* endswitch */
  72.  
  73.     response (me);
  74.  
  75.     HFILE com_port;
  76.  
  77.  
  78.     switch (device) {
  79.     case 1: // Speaker
  80.        me.vmSetLine(MODEM_ENGINE::speaker);
  81.        response (me);
  82.  
  83.        break;
  84.  
  85.      case 2: {// Handset
  86.        me.vmSetLine(MODEM_ENGINE::phone);
  87.        response (me);
  88.  
  89.        cout << endl << "Pick up the handset." << endl << endl;
  90.  
  91.        char c;
  92.        c= me.mWaitForDLECode();
  93.        while (c != 't') {
  94.           c= me.mWaitForDLECode();
  95.        } /* endwhile */
  96.        }
  97.        break;
  98.  
  99.      case 3: // Phone line
  100.        me.vmSetLine(MODEM_ENGINE::line);
  101.        response (me);
  102.  
  103.        me.mAnswer();
  104.        response (me);
  105.  
  106.        break;
  107.  
  108.      case 4: // Phone line monitor
  109.        me.vmSetLine(MODEM_ENGINE::lineMonitor);
  110.        response (me);
  111.  
  112.        me.mAnswer();
  113.        response (me);
  114.  
  115.        break;
  116.  
  117.     } /* endswitch */
  118.  
  119.     me.vmTransmit(com_port);
  120.     response (me);
  121.  
  122.  
  123.     unsigned char buf[1024];
  124.     ULONG cbWritten=0, bitcount=0;
  125.  
  126.     cout << "Initializing done..." << endl << "Playing file..." << endl;
  127.  
  128.     while (!fin.eof()) {
  129.  
  130.        fin.read(buf, sizeof(buf));
  131.        bitcount+= fin.gcount();
  132.        DosWrite(com_port, &buf, fin.gcount(), &cbWritten);
  133.     }
  134.  
  135.     cout << bitcount << " bytes written" << endl;
  136.  
  137.     MODEM_ENGINE::MODEMRESPONSE mr;
  138.     me.vmStopTransmit();
  139.     mr= response (me, 50);
  140.  
  141.     while (mr.code==MODEM_ENGINE::none) {
  142.        me.vmStopTransmit();
  143.        mr= response (me, 50);
  144.     } /* endwhile */
  145.  
  146.     cout << "Playing done..." << endl << "Resetting the modem..." << endl;
  147.  
  148.     me.mInitialize();
  149.     response (me);
  150.  
  151.     cout << "All done..." << endl;
  152.  
  153.  
  154.  } else {
  155.     cout << endl << "Unable to open the '" << com_port << "' device." << endl;
  156.  }
  157. }
  158.  
  159.  
  160. //*****************************************************************************
  161. MODEM_ENGINE::MODEMRESPONSE response (MODEM_ENGINE & me, int timeout) {
  162.  
  163.  MODEM_ENGINE::MODEMRESPONSE mr;
  164.  
  165.     do {
  166.        mr= me.mWaitForModemResponse (timeout);
  167.        if (debug) cout << mr.verbose << endl;
  168.        if (mr.verbose[0]== 0) {
  169.           mr.code= MODEM_ENGINE::none;
  170.        }
  171.     } while ((mr.code != MODEM_ENGINE::ok) &&
  172.              (mr.code != MODEM_ENGINE::error) &&
  173.              (mr.code != MODEM_ENGINE::connect) &&
  174.              (mr.code != MODEM_ENGINE::none) &&
  175.              (mr.code != MODEM_ENGINE::vcon));
  176.  
  177.    return mr;
  178. }
  179.  
  180. //*****************************************************************************
  181. void parse_arg(int argc, char * argv[]) {
  182.  
  183.  for (int i=1; i< argc; i++) {
  184.  
  185.     if (!strncmp(argv[i], "-d:", 3)) {
  186.       device= atoi(argv[i]+3);
  187.       if (device != 1 && device != 2 && device != 3 && device != 4) {
  188.          usage();
  189.          exit(0);
  190.       }
  191.     } else {
  192.      if (!strncmp(argv[i], "-b:", 3)) {
  193.         bits_per_sample= atoi(argv[i]+3);
  194.         if (bits_per_sample != 2 && bits_per_sample != 3 && bits_per_sample != 4) {
  195.            usage();
  196.            exit(0);
  197.         }
  198.         cout << argv[i]+3 << "\n";
  199.      } else {
  200.         if (!strncmp(argv[i], "-f:", 3)) {
  201.  
  202.           file_name= strdup(argv[i]+3);
  203.         } else {
  204.            if (!strncmp(argv[i], "-c:", 3)) {
  205.  
  206.               com_port= strdup(argv[i]+3);
  207.            } else {
  208.               if (!strncmp(argv[i], "-de", 3)) {
  209.  
  210.                  debug= TRUE;
  211.               } else {
  212.  
  213.                 usage();
  214.                 exit(0);
  215.  
  216.               } /* endif */
  217.            } /* endif */
  218.         } /* endif */
  219.      } /* endif */
  220.     } /* endif */
  221.  } /* endfor */
  222.  
  223.  if (file_name==NULL || com_port==NULL) {
  224.     usage();
  225.     exit(0);
  226.  } else {
  227.  } /* endif */
  228.  
  229. };
  230.  
  231.  
  232. //*****************************************************************************
  233. void usage(){
  234.  
  235.  cout << "* Required parameters: " << endl << endl
  236.       << "-f:filename, where 'filename' is the name (+ path) of the file to be played." << endl << endl
  237.       << "-c:comport, where 'comport' is the name of the device the voice modem is" << endl << " connected to." << endl << endl
  238.       << "* Optional parameters:" << endl << endl
  239.       << "-b:bits, where 'bits' is either 2, 3 or 4 and stands for the sampling rate of" << endl << " the file to be played, this defaults to 4" << endl << endl
  240.       << "-d:device, where 'device' is either 1, 2, 3 or 4." << endl << " 1 stands for modem speaker" << endl << " 2 stands for phone handset." << endl << " 3 stands for telephone line. This defaults to 1" << endl << " 4 stands for telephone line with monitor. This defaults to 1" << endl << endl
  241.       << "-debug turns debug info on." << endl << endl
  242.       << "* Example:" << endl << endl
  243.       << "play -f:c:\\path\\file.msg -c:com1 -b:3 -d:2" << endl;
  244. };
  245.